home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / citsrc6K05.lha / virt2.c < prev    next >
C/C++ Source or Header  |  1996-08-29  |  12KB  |  496 lines

  1. /*
  2. *                virt2.c
  3. *
  4. * Virtual room handler for Citadel-86, part 2.
  5. */
  6. #include "ctdl.h"
  7. /*
  8. *                history
  9. *
  10. * 91Nov15 HAW Restructured.
  11. * 89Apr04 HAW Created.
  12. */
  13. /*
  14. *                contents
  15. *
  16. *    VNeedCall()        Checks to see if a node needs a call
  17. *    VirtRoomRoutable()    Checks to see if a room is routable
  18. *    SendVirtual()        Sends a virtual room to another system
  19. *    RecVirtualRoom()    Receives a virtual room
  20. *    SetUpForVirtuals()    Gets set up for a virtual room reception
  21. *    DoVirtuals()        Go through all shared virtual rooms for a sys
  22. *    SendVirtualRoom()    Manages sending a virtual room out
  23. *    DumpVRoom()        Dumps a shared virtual room setup on console
  24. *    UnCacheVirtualRoom()    Unsets the cache bit
  25. *    VRNeedCall()        Check to see if there is outgoing for a room
  26. *    VirtualRoomOutgoing()    Same?
  27. *    VirtualRoomMap()    Sends the room/file map for a virtual room
  28. *    CacheVirtualRoom()    Caches a virtual room
  29. *
  30. */
  31. extern VirtualRoom *VRoomTab;
  32. extern VirtNet     *VirtNetList;
  33. extern char VirtualInUse;
  34. extern int  VirtSize, VNetSize;
  35. extern NetBuffer netBuf;
  36. extern CONFIG    cfg;
  37. extern int       thisNet;
  38. extern FILE      *netLog;
  39. extern char      inNet, netDebug, TrError;
  40. extern char     *READ_ANY, *APPEND_ANY;
  41. extern NetTable  *netTab;
  42. static int SendVirtualNormalMessages(int VirtIndex, int VirtNo,int (*SendFunc)(int c));
  43. static char VNeedSend(int system, int rover, int which, char NotPeon);
  44. static char VirtualOutgoingMessages(VirtPoint *VirtP);
  45. static int ThrowAll(int which, char *distance, MSG_NUMBER start, MSG_NUMBER end,
  46. int (*SendFunc)(int c));
  47. /*
  48. * VNeedCall()
  49. *
  50. * This function checks to see if list element needs calling.
  51. */
  52. char VNeedCall(VirtPoint *VirtP)
  53.   {
  54.   #ifndef NO_VIRTUAL_ROOMS
  55.   switch (VGetMode(VirtP->mode))
  56.     {
  57.     /*
  58.     * For active backbone, we assume that we are always called via
  59.     * roomsShared(), which will keep us from calling more than once.
  60.     * Since an Active should always call once, we return TRUE here
  61.     * without actually checking values.
  62.     */
  63.     case ACTIVE_BACKBONE:
  64.     return (char)(inNet == NORMAL_NET);
  65.     case PASS_BACKBONE:   return FALSE;
  66.     case PEON:
  67.     return VirtualOutgoingMessages(VirtP);
  68.  
  69.     }
  70.   return FALSE;
  71.   #else
  72.   return FALSE;
  73.   #endif
  74.  
  75.   }
  76. /*
  77. * VirtualOutgoingMessages()
  78. *
  79. * Are there outgoing messages, regardless of caching & room status?
  80. */
  81. static char VirtualOutgoingMessages(VirtPoint *VirtP)
  82.   {
  83.   if (VirtP->LDSent < VRoomTab[VirtP->WhichVirt].vrHiLD ||
  84.   VGetFA(VirtP->mode))
  85.   return TRUE;
  86.   if (VGetMode(VirtP->mode) != PEON)
  87.   if (VirtP->LocSent < VRoomTab[VirtP->WhichVirt].vrHiLocal)
  88.   return TRUE;
  89.   return FALSE;
  90.  
  91.   }
  92. /*
  93. * VirtRoomRoutable()
  94. *
  95. * This checks to see if the given room is the one we're looking for as
  96. * specified in the d parameter.
  97. */
  98. int VirtRoomRoutable(VirtualRoom *room, int system, int index, int which,
  99. void *d)
  100.   {
  101.   RoomSearch *arg;
  102.   arg = d;
  103.   if (strCmpU(room->vrName, arg->Room) == SAMESTRING)
  104.     {
  105.     arg->virtual = TRUE;
  106.     arg->room = which;
  107.     arg->index = index;
  108.     arg->reason = FOUND;
  109.     return ERROR;
  110.  
  111.     }
  112.   return TRUE;
  113.  
  114.   }
  115. /*
  116. * SendVirtual()
  117. *
  118. * This function manages sending a room to another system.
  119. */
  120. int SendVirtual(int VirtIndex, char *d1, char *d2, char *d3)
  121.   {
  122.   char    work[15];
  123.   int        VirtNo, count = 0;
  124.   VirtNo = VirtNetList[thisNet].VirtList[VirtIndex].WhichVirt;
  125.   if (VGetFA(VirtNetList[thisNet].VirtList[VirtIndex].mode))
  126.     {
  127.     sPrintf(work, V_CACHE_END_NAME, VirtIndex);
  128.     if (SendPrepAsNormal(work, &count))
  129.     VUnSetFA(VirtNetList[thisNet].VirtList[VirtIndex].mode);
  130.  
  131.     }
  132.   splitF(netLog, "Sending %s (virtual) ", VRoomTab[VirtNo].vrName);
  133.   count += SendVirtualNormalMessages(VirtIndex, VirtNo, sendITLchar);
  134.   return count;
  135.  
  136.   }
  137. /*
  138. * SendVirtualNormalMessages()
  139. *
  140. * Sends normal virtual messages to some destination.
  141. */
  142. static int SendVirtualNormalMessages(int VirtIndex, int VirtNo,
  143. int (*SendFunc)(int c))
  144.   {
  145.   int        count;
  146.   MSG_NUMBER    StartMsg;
  147.   /* Send all the new LD messages received */
  148.   StartMsg = VirtNetList[thisNet].VirtList[VirtIndex].LDSent;
  149.   count=ThrowAll(VirtNo, LD_DIR, StartMsg, VRoomTab[VirtNo].vrHiLD, SendFunc);
  150.   if (TrError == TRAN_SUCCESS)
  151.   VirtNetList[thisNet].VirtList[VirtIndex].LDSent =
  152.   VRoomTab[VirtNo].vrHiLD;
  153.   if (VGetMode(VirtNetList[thisNet].VirtList[VirtIndex].mode) != PEON)
  154.     {
  155.     StartMsg = VirtNetList[thisNet].VirtList[VirtIndex].LocSent;
  156.     count += ThrowAll(VirtNo, LOCAL_DIR, StartMsg,
  157.     VRoomTab[VirtNo].vrHiLocal, SendFunc);
  158.     if (TrError == TRAN_SUCCESS)
  159.     VirtNetList[thisNet].VirtList[VirtIndex].LocSent =
  160.     VRoomTab[VirtNo].vrHiLocal;
  161.  
  162.     }
  163.   VRoomTab[VirtNo].vrChanged |= SENT_DATA;
  164.   return count;
  165.  
  166.   }
  167. /*
  168. * ThrowAll()
  169. *
  170. * This function sends a virtual room to another system.
  171. */
  172. static int ThrowAll(int which, char *distance, MSG_NUMBER start, MSG_NUMBER end,
  173. int (*SendFunc)(int c))
  174.   {
  175.   #ifndef NO_VIRTUAL_ROOMS
  176.   MSG_NUMBER  rover;
  177.   int        count=0;
  178.   char    fn[100];
  179.   extern FILE *netMisc;
  180.   extern PROTO_TABLE Table[];
  181.   extern int    TransProtocol;
  182.   for (rover = start + 1; rover <= end; rover++)
  183.     {
  184.     CreateVAName(fn, which, distance, rover);
  185.     if ((netMisc = safeopen(fn, READ_ANY)) != NULL)
  186.       {
  187.       while (getMessage(getNetChar, TRUE, FALSE, TRUE))
  188.         {
  189.         count++;
  190.         prNetStyle(1, SendFunc, TRUE, "");
  191.  
  192.         }
  193.       fclose(netMisc);
  194.  
  195.       }
  196.  
  197.     }
  198.   return count;
  199.   #else
  200.   return 0;
  201.   #endif
  202.  
  203.   }
  204. /*
  205. * RecVirtualRoom()
  206. *
  207. * This function receives a virtual room directly from another system.
  208. */
  209. int RecVirtualRoom(int VirtIndex, char ReplyFirst)
  210.   {
  211.   #ifndef NO_VIRTUAL_ROOMS
  212.   int        VirtNo;
  213.   char    fn[50];
  214.   extern FILE *upfd;
  215.   extern char *W_R_ANY;
  216.   SetUpForVirtuals(VirtIndex, &VirtNo, fn);
  217.   splitF(netLog, "Receiving %s (virtual)\n", VRoomTab[VirtNo].vrName);
  218.   return (ITL_StartRecMsgs(fn, ReplyFirst, TRUE, NULL) == ITL_SUCCESS)
  219.   ? TRUE : FALSE;
  220.   #else
  221.   return TRUE;
  222.   #endif
  223.  
  224.   }
  225. /*
  226. * SetUpForVirtuals()
  227. *
  228. * This sets up a filename for a virtual room.
  229. */
  230. void SetUpForVirtuals(int VirtIndex, int *VirtNo, char *fn)
  231.   {
  232.   MSG_NUMBER  rover;
  233.   char *distance;
  234.   *VirtNo = VirtNetList[thisNet].VirtList[VirtIndex].WhichVirt;
  235.   if (VGetMode(VirtNetList[thisNet].VirtList[VirtIndex].mode) != PEON)
  236.     {
  237.     distance = LD_DIR;
  238.     rover = VRoomTab[*VirtNo].vrHiLD + 1l;
  239.     VRoomTab[*VirtNo].vrChanged |= LD_CHANGE;
  240.  
  241.     }
  242.   else
  243.     {
  244.     distance = LOCAL_DIR;
  245.     rover = VRoomTab[*VirtNo].vrHiLocal + 1l;
  246.     VRoomTab[*VirtNo].vrChanged |= LOC_CHANGE;
  247.  
  248.     }
  249.   CreateVAName(fn, *VirtNo, distance, rover);
  250.  
  251.   }
  252. static char VirtualCheck(int system, int rover, int which, int *cmd);
  253. /*
  254. * DoVirtuals()
  255. *
  256. * This function sends rooms to another system, if needed.
  257. */
  258. void DoVirtuals(int system,
  259. int (*virtfunc)(VirtualRoom *room, int system, int index, int which, void *d),
  260. void *d)
  261.   {
  262.   #ifndef NO_VIRTUAL_ROOMS
  263.   int rover, which;
  264.   if (!VirtualInUse) return ;
  265.   for (rover = 0; rover < VIRT_LIMIT; rover++)
  266.     {
  267.     which = VirtNetList[system].VirtList[rover].WhichVirt;
  268.     if (which >= VirtSize || which < 0 || !VRoomInuse(which))
  269.       {
  270.       VirtNetList[system].VirtList[rover].WhichVirt = -1;
  271.       continue;
  272.  
  273.       }
  274.     if ((*virtfunc)(VRoomTab + which, system, rover, which, d) == ERROR)
  275.     break;
  276.  
  277.     }
  278.   #endif
  279.  
  280.   }
  281. /*
  282. * SendVirtualRoom()
  283. *
  284. * This function transfers a virtual room's contents to another system.
  285. */
  286. int SendVirtualRoom(VirtualRoom *room, int system, int index, int which,void *d)
  287.   {
  288.   char doit;
  289.   int  cmd;
  290.   extern char        MassTransferSent;
  291.   if (!gotCarrier()) return ERROR;
  292.   doit = VirtualCheck(system, index, which, &cmd);
  293.   if (doit && !MassTransferSent)
  294.     {
  295.     ITL_optimize(TRUE);
  296.     findAndSend(cmd, NULL, NULL, NULL, index, SendVirtual,
  297.     room->vrName, RecVirtualRoom);
  298.  
  299.     }
  300.   return TRUE;
  301.  
  302.   }
  303. /*
  304. * VirtualCheck()
  305. *
  306. * This handles deciding if a room should be sent.
  307. */
  308. static char VirtualCheck(int system, int index, int which, int *cmd)
  309.   {
  310.   char doit;
  311.   extern char inReceive;
  312.   doit = TRUE;
  313.   switch (VGetMode(VirtNetList[system].VirtList[index].mode))
  314.     {
  315.     case PEON:
  316.     *cmd = NET_ROOM;
  317.     if (VirtNetList[system].VirtList[index].LDSent >=
  318.     VRoomTab[which].vrHiLD &&
  319.     !VGetFA(VirtNetList[system].VirtList[index].mode))
  320.     doit = FALSE;
  321.     break;
  322.     case PASS_BACKBONE:
  323.     if (!inReceive || VRoomTab[which].vrChanged & SENT_DATA)
  324.     doit = FALSE;
  325.     else
  326.     *cmd = NET_ROOM;
  327.     break;
  328.     case ACTIVE_BACKBONE:
  329.     *cmd = (netBuf.nbflags.local) ? NET_ROOM : NET_ROUTE_ROOM;
  330.     break;
  331.     default:
  332.     splitF(netLog, "Error in virtuals for %s!\n", VRoomTab[which].vrName);
  333.     doit = ERROR;
  334.  
  335.     }
  336.   return doit;
  337.  
  338.   }
  339. /*
  340. * VNeedSend()
  341. *
  342. * Determines if we need to send messages in a virtual regardless of the
  343. * mode, except we know it's a backbone situation (so check both local and
  344. * backbones).
  345. */
  346. static char VNeedSend(int system, int rover, int which, char NotPeon)
  347.   {
  348.   return(char) (VirtNetList[system].VirtList[rover].LDSent <
  349.   VRoomTab[which].vrHiLD ||
  350.   (NotPeon && VirtNetList[system].VirtList[rover].LocSent <
  351.   VRoomTab[which].vrHiLocal));
  352.  
  353.   }
  354. /*
  355. * DumpVRoom()
  356. *
  357. * This function dumps information concerning a virtual room.
  358. */
  359. int DumpVRoom(VirtualRoom *room, int system, int index, int which, void *d)
  360.   {
  361.   #ifndef NO_VIRTUAL_ROOMS
  362.   mPrintf("%s: ", room->vrName);
  363.   switch (VGetMode(VirtNetList[system].VirtList[index].mode))
  364.     {
  365.     case PEON: mPrintf("PEON"); break;
  366.     case ACTIVE_BACKBONE:
  367.     mPrintf("ACTIVE BACKBONE"); break;
  368.     case PASS_BACKBONE:
  369.     mPrintf("PASSIVE BACKBONE"); break;
  370.  
  371.     }
  372.   mPrintf(" relationship (last bb sent %ld, hi bb %ld",
  373.   VirtNetList[system].VirtList[index].LDSent, room->vrHiLD);
  374.   if (VGetMode(VirtNetList[system].VirtList[index].mode) != PEON)
  375.     {
  376.     mPrintf(", last peon sent %ld, hi peon %ld",
  377.     VirtNetList[system].VirtList[index].LocSent, room->vrHiLocal);
  378.  
  379.     }
  380.   mPrintf("):%d", VirtNetList[system].VirtList[index].mode);
  381.   if (VGetFA(VirtNetList[system].VirtList[index].mode))
  382.   mPrintf(" *");
  383.   mPrintf("\n ");
  384.   return TRUE;
  385.   #endif
  386.  
  387.   }
  388. /*
  389. * UnCacheVirtualRoom()
  390. *
  391. * Unset the flag indicating the room is cached for this system.
  392. */
  393. int UnCacheVirtualRoom(VirtualRoom *room, int system, int index, int which,
  394. void *d)
  395.   {
  396.   VUnSetFA(VirtNetList[system].VirtList[index].mode);
  397.   return TRUE;
  398.  
  399.   }
  400. /*
  401. * VRNeedCall()
  402. *
  403. * This discerns if the given virtual room has outgoing.
  404. */
  405. int VRNeedCall(VirtualRoom *room, int system, int index, int which,
  406. char *d)
  407.   {
  408.   if (VNeedCall(VirtNetList[system].VirtList + index))
  409.     {
  410.     *d = TRUE;
  411.     return ERROR;
  412.  
  413.     }
  414.   return TRUE;
  415.  
  416.   }
  417. /*
  418. * VirtualRoomOutgoing()
  419. *
  420. * This checks to see if the given virtual room has outgoing stuff, regardless
  421. * of room backbone status (we're already connected).
  422. */
  423. int VirtualRoomOutgoing(VirtualRoom *room, int system, int index, int which,
  424. char *d)
  425.   {
  426.   if (VirtualOutgoingMessages(VirtNetList[system].VirtList + index))
  427.     {
  428.     *d = TRUE;
  429.     return ERROR;
  430.  
  431.     }
  432.   return TRUE;
  433.  
  434.   }
  435. /*
  436. * VirtualRoomMap()
  437. *
  438. * This sends the name and cache file name of a virtual room.
  439. */
  440. int VirtualRoomMap(VirtualRoom *room, int system, int index, int which,
  441. void *d)
  442.   {
  443.   char work[20], NotPeon;
  444.   NotPeon = (VGetMode(VirtNetList[system].VirtList[index].mode) != PEON);
  445.   if (VGetFA(VirtNetList[system].VirtList[index].mode) ||
  446.   VNeedSend(system, index, which, NotPeon))
  447.     {
  448.     ITL_Line(room->vrName);
  449.     sprintf(work, V_CACHE_END_NAME, index);
  450.     ITL_Line(work);
  451.  
  452.     }
  453.   return TRUE;
  454.  
  455.   }
  456. /*
  457. * CacheVirtualRoom()
  458. *
  459. * This function should cache a virtual room.
  460. */
  461. int CacheVirtualRoom(VirtualRoom *room, int sys, int index, int which, void *d)
  462.   {
  463.   int        count;
  464.   char    work[15], NotPeon;
  465.   char    tempNm[3*NAMESIZE];
  466.   extern char PrTransmit, CacheUpdated;
  467.   PrTransmit = FALSE;
  468.   NotPeon = (VGetMode(VirtNetList[sys].VirtList[index].mode) != PEON);
  469.   if (VNeedSend(sys, index, which, NotPeon))
  470.     {
  471.     if (sys != thisNet)
  472.     getNet(sys, &netBuf);
  473.     sPrintf(work, V_CACHE_END_NAME, index);
  474.     NetCacheName(tempNm, thisNet, work);
  475.     if ((upfd = safeopen(tempNm, APPEND_ANY)) != NULL)
  476.       {
  477.       count = SendVirtualNormalMessages(index, which, putFLChar);
  478.       fclose(upfd);
  479.       if (count == 0 &&
  480.       !VGetFA(VirtNetList[sys].VirtList[index].mode))
  481.       unlink(tempNm);
  482.       else
  483.         {
  484.         VSetFA(VirtNetList[sys].VirtList[index].mode);
  485.         if (count) CacheUpdated = TRUE;
  486.  
  487.         }
  488.  
  489.       }
  490.  
  491.     }
  492.   PrTransmit = TRUE;
  493.   return TRUE;
  494.  
  495.   }
  496.